package com.team.appinfo.controller.admin;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

@Controller
public class XiaZaiZiYuanController {
    @RequestMapping("xiazaiwenjian")
    public void xiazaiwenjian(String downloadlink,
                              String apkfilename,
                              HttpServletRequest request,
                              HttpServletResponse response){
        try {
            //读取下载文件的内容
            FileInputStream fis=new FileInputStream(downloadlink);
            byte [] bs=new byte[fis.available()];  //fis.available()表求文件大小
            fis.read(bs);
            fis.close();
            //设置下载的文件头
            response.setHeader("Content-Disposition","attachment;fileName="+apkfilename);
            //设置文件的类型
            response.setContentType("application/vnd.android.package-archive");  //设置下载apk的类型
            //将数据写入到响应流中
            ServletOutputStream sos=response.getOutputStream();
            sos.write(bs);
            sos.flush();
            sos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
